package hibernate.dao;

import java.util.List;

import hibernate.entity.Funcionario;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.hibernate.mapping.FetchProfile.Fetch;
import org.objectweb.asm.commons.TryCatchBlockSorter;

public class FuncionarioDAO {

	private Session session;

	public FuncionarioDAO() {

	}

	public void adiciona(Funcionario funcionario) {
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();
		Transaction transaction = session.beginTransaction();

		try {
			session.save(funcionario);
			transaction.commit();
		} catch (Exception e) {

			if (transaction != null) {
				transaction.rollback();
				System.out.println("Erro: " + e);
			}

		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();

		}

	}

	public void alterar(Funcionario funcionario) {

		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();
		Transaction transaction = session.beginTransaction();

		try {

			session.update(funcionario);
			transaction.commit();

		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
				System.out.println("Erro: " + e);
			}
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}

	}
	
	
	public void remover(Funcionario funcionario) {

		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();
		Transaction transaction = session.beginTransaction();

		try {
			session.delete(funcionario);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
				System.out.println("Erro: " + e);
			}
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}

	}
	
	public Funcionario importaDados(String nome){
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();
		Criteria criteria = session.createCriteria(Funcionario.class);
		Funcionario funcionario = null;
		
		try {
			criteria.setFetchMode("especialidades",FetchMode.JOIN );
			criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			criteria.add(Restrictions.eq("nome", nome));
			funcionario = (Funcionario) criteria.uniqueResult();
			
		} catch (Exception e) {
			System.out.println("Erro: " + e);
		}finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
				
		return funcionario;
	}	
	
	public List<Funcionario> getLista(){
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();
		Criteria criteria = session.createCriteria(Funcionario.class);
		List<Funcionario> listaFuncionarios = null;
		
		try {
			criteria.setFetchMode("especialidades",FetchMode.JOIN );
			criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			listaFuncionarios = (List<Funcionario>) criteria.list();
		} catch (Exception e) {
			System.out.println("Erro: " + e);
		}finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
		
		return listaFuncionarios;
	}

}
